//需要用到的函数
let InsertString = (t, c, n) => {
  let r = new Array();
  for (let i = 0; i * 2 < t.length; i++) {
    r.push(t.substr(i * 2, n));
  }
  return r.join(c);
}
//需要用到的函数
let FillString = (t, c, n, b) => {
  if ((t == "") || (c.length != 1) || (n <= t.length)) {
    return t;
  }
  let l = t.length;
  for (let i = 0; i < n - l; i++) {
    if (b == true) {
      t = c + t;
    }
    else {
      t += c;
    }
  }
  return t;
}

/**
 * 16进制转浮点数
 * @param t
 * @returns {string|number}
 * @constructor
 */
let HexToSingle = t => {
  t = t.replace(/\s+/g, "");
  if (t == "") {
    return "";
  }
  if (t == "00000000") {
    return "0";
  }
  if ((t.length > 8) || (isNaN(parseInt(t, 16)))) {
    return "Error";
  }
  if (t.length < 8) {
    t = FillString(t, "0", 8, true);
  }
  t = parseInt(t, 16).toString(2);
  t = FillString(t, "0", 32, true);
  let s = t.substring(0, 1);
  let e = t.substring(1, 9);
  let m = t.substring(9);
  e = parseInt(e, 2) - 127;
  m = "1" + m;
  if (e >= 0) {
    m = m.substr(0, e + 1) + "." + m.substring(e + 1)
  }
  else {
    m = "0." + FillString(m, "0", m.length - e - 1, true)
  }
  if (m.indexOf(".") == -1) {
    m = m + ".0";
  }
  let a = m.split(".");
  let mi = parseInt(a[0], 2);
  let mf = 0;
  for (let i = 0; i < a[1].length; i++) {
    mf += parseFloat(a[1].charAt(i)) * Math.pow(2, -(i + 1));
  }
  m = parseInt(mi) + parseFloat(mf);
  if (s == 1) {
    m = 0 - m;
  }
  return m;
}

/**
 * 浮点数转16进制
 */
let SingleToHex = t => {
  if (t == "") {
    return "";
  }
  t = parseFloat(t);
  if (isNaN(t) == true) {
    return "Error";
  }
  if (t == 0) {
    return "00000000";
  }
  let s,
    e,
    m;
  if (t > 0) {
    s = 0;
  }
  else {
    s = 1;
    t = 0 - t;
  }
  m = t.toString(2);
  if (m >= 1) {
    if (m.indexOf(".") == -1) {
      m = m + ".0";
    }
    e = m.indexOf(".") - 1;
  }
  else {
    e = 1 - m.indexOf("1");
  }
  if (e >= 0) {
    m = m.replace(".", "");
  }
  else {
    m = m.substring(m.indexOf("1"));
  }
  if (m.length > 24) {
    m = m.substr(0, 24);
  }
  else {
    m = FillString(m, "0", 24, false)
  }
  m = m.substring(1);
  e = (e + 127).toString(2);
  e = FillString(e, "0", 8, true);
  let r = parseInt(s + e + m, 2).toString(16);
  r = FillString(r, "0", 8, true);
  return InsertString(r, " ", 2).toUpperCase();
}

/**
 * 十六进制转十进制
 * @param t
 */
let hexToDec = t => {
  if (t) {
    t = t.replace(/\s/g, "");
    return parseInt(t, 16)
  }
  return null;
}

export {HexToSingle, SingleToHex}
